package com.github.ecbp.store.service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.ecbp.common.data.entity.BaseEntity;
import com.github.ecbp.common.data.service.impl.BaseServiceImpl;
import com.github.ecbp.common.data.utils.TransformUtils;
import com.github.ecbp.store.service.dto.ProductCategoryDTO;
import com.github.ecbp.store.service.entity.ProductCategory;
import com.github.ecbp.store.service.mapper.ProductCategoryMapper;
import com.github.ecbp.store.service.service.ProductCategoryService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

/**
 * @Author Created by Alex·He on 2020/4/28 15:52
 * @Version 1.0
 * @Description 商品分类表(ProductCategory)表服务实现类
 */
@Service
public class ProductCategoryServiceImpl extends BaseServiceImpl<ProductCategoryMapper, ProductCategory>
        implements ProductCategoryService {

    /**
     *功能描述：
     * @author Created by Alex·He on 2020/4/29 10:17
     * @version 1.0
     * @description 保存商品分类，包含更新Level
     */
    @Override
    @Transactional(rollbackFor = Throwable.class)
    public boolean save(ProductCategoryDTO param) {
        ProductCategory category = TransformUtils.copy(param, ProductCategory.class);
        updateLevel(category);
        return super.saveBase(category);
    }

    /**
     *功能描述：
     * @author Created by Alex·He on 2020/4/29 10:27
     * @version 1.0
     * @description 更新商品分类，包含更新Level
     */
    @Override
    public boolean updateById(ProductCategoryDTO param) {
        ProductCategory category = TransformUtils.copy(param, ProductCategory.class);
        updateLevel(category);
        return super.updateByIdBase(category);
    }

    /**
     *功能描述：
     * @author Created by Alex·He on 2020/4/28 17:12
     * @version 1.0
     * @description 修改商品分类显示状态
     */
    @Override
    public boolean updateShowStatus(Long id, Integer showStatus) {
        ProductCategory category = new ProductCategory();
        category.setId(id);
        category.setShowStatus(showStatus);
        return super.updateByIdBase(category);
    }

    /**
     *功能描述：
     * @author Created by Alex·He on 2020/4/28 17:18
     * @version 1.0
     * @description 修改商品分类的导航栏显示状态
     */
    @Override
    public boolean updateNavStatus(Long id, Integer navStatus) {
        ProductCategory category = new ProductCategory();
        category.setId(id);
        category.setNavStatus(navStatus);
        return super.updateByIdBase(category);
    }

    /**
     *功能描述：
     * @author Created by Alex·He on 2020/4/29 10:21
     * @version 1.0
     * @description 修改商品分类层级
     */
    private void updateLevel(ProductCategory category) {
        if (category.getParentId() == 0) {
            //没有父分类时为一级菜单
            category.setLevel(0);
        } else {
            //有父分类时选择根据父分类的level设置
            ProductCategory parentCategory = baseMapper.selectById(category.getParentId());
            if (parentCategory != null) {
                category.setLevel(parentCategory.getLevel() + 1);
            } else {
                category.setLevel(0);
            }
        }
    }

    /**
     *功能描述：
     * @author Created by Alex·He on 2020/4/28 18:03
     * @version 1.0
     * @description 模糊查询的参数组合，可通过分类名称、分类级别、关键字、是否显示在导航栏、显示状态进行单独或者联合查询
     */
    @Override
    protected void assemble(ProductCategory entity, LambdaQueryWrapper<ProductCategory> wrapper) {
        super.assemble(entity, wrapper);
        if (!StringUtils.isEmpty(entity.getName())) {
            wrapper.likeRight(ProductCategory::getName, entity.getName());
        }
        if (!ObjectUtils.isEmpty(entity.getLevel())) {
            wrapper.likeRight(ProductCategory::getLevel, entity.getLevel());
        }
        if (!StringUtils.isEmpty(entity.getKeyword())) {
            wrapper.likeRight(ProductCategory::getKeyword, entity.getKeyword());
        }
        if (!ObjectUtils.isEmpty(entity.getNavStatus())) {
            wrapper.likeRight(ProductCategory::getNavStatus, entity.getNavStatus());
        }
        if (!ObjectUtils.isEmpty(entity.getShowStatus())) {
            wrapper.likeRight(ProductCategory::getShowStatus, entity.getShowStatus());
        }
        wrapper.orderByDesc(BaseEntity::getCreateTime);
    }
}
